package com.smart.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.smart.model.MaintenanceManInfo;
import com.smart.util.DBUtil;

public class MaintenanceManInfoDAO {
	public List<MaintenanceManInfo> getMaintenanceManInfoList() throws SQLException {
		Connection conn = DBUtil.getConnection();
		String sql = "select id, name, cellphone, workload from maintenance_man_info";
		PreparedStatement pSt = conn.prepareCall(sql);
		ResultSet rs = pSt.executeQuery();
		List<MaintenanceManInfo> list = new ArrayList<MaintenanceManInfo>();
		while (rs.next()) {
			MaintenanceManInfo info = new MaintenanceManInfo();
			info.setId(rs.getInt(1));
			info.setName(rs.getString(2));
			info.setCellphone(rs.getString(3));
			info.setWorkload(rs.getInt(4));
			list.add(info);
		}

		return list;
	}

	public void dispatchTask(int maintenanceManID) throws SQLException {
		Connection conn = DBUtil.getConnection();
		String sql = "update maintenance_man_info set workload=workload+1 where id=?";
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setInt(1, maintenanceManID);
		pSt.execute();
	}

	public void finishTask(int maintenanceManID) throws SQLException {
		Connection conn = DBUtil.getConnection();
		String sql = "update maintenance_man_info set workload=workload-1 where id=?";
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setInt(1, maintenanceManID);
		pSt.execute();
	}
}
